﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports System.IO
Imports Boogaloo.Core
Imports Boogaloo.Core.BLL
Imports Boogaloo.Core.UI
Imports Boogaloo.Admin.UI.Controls

Namespace UI.Webpages


    Partial Public Class UserControlsRegistry
        Inherits BaseAdminPage

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            If Not Page.IsPostBack Then
                chk_has_editor.Checked = False
                pnl_editor.Visible = False
                Dim dirs As String() = Groove.GetFullDirectoryTree(Server.MapPath(CoreSettings.GetSetting("SITE_ControlsDirectory")))
                Dim dirName As String = String.Empty
                For Each dirName In dirs
                    Dim dn As String = Groove.RewriteServerPathAsVirtualPath(dirName)
                    ddl_dir.Items.Add(New ListItem(dn))
                    ddl_dir_editor.Items.Add(New ListItem(dn))
                Next

                BindPage()
            End If

        End Sub


        Private Sub BindPage()

            pnl_specifichost.Visible = True

            Dim dirFiles As String() = Directory.GetFiles(Server.MapPath("~" & ddl_dir_editor.SelectedValue), "*.ascx")

            Dim display As New ArrayList
            Dim rc As RegisteredControl = Nothing
            For Each rc In Groove.admin_root.registered_controls
                display.Add(Server.MapPath(rc.ascx_source))
                If Not String.IsNullOrEmpty(rc.ascx_source_editor) Then display.Add(Server.MapPath(rc.ascx_source_editor))
            Next

            Dim fn As String = String.Empty
            For Each fn In dirFiles
                If Not display.Contains(fn) Then
                    Dim fi As New FileInfo(fn)
                    If ddl_dir.SelectedValue = ddl_dir_editor.SelectedValue Then ddl_ascx.Items.Add(New ListItem(fi.Name))
                    ddl_ascx_editor.Items.Add(New ListItem(fi.Name))
                End If
            Next

            If ddl_ascx_editor.Items.Count = 0 Then
                ddl_ascx_editor.Visible = False
                lit_no_ascx_editor.Visible = True
            Else
                ddl_ascx_editor.Visible = True
                lit_no_ascx_editor.Visible = False
            End If

            If ddl_ascx.Items.Count = 0 Then
                ddl_ascx.Visible = False
                lb_add.Enabled = False
                lit_no_ascx.Visible = True
            Else
                lb_add.Enabled = True
                ddl_ascx.Visible = True
                lit_no_ascx.Visible = False
            End If

            ' Mindfly standards names the editor version of the control ... see if it is there 
            If ddl_ascx_editor.Items.Count > 0 And ddl_ascx.Items.Count > 0 Then
                If ddl_ascx_editor.Items.Contains(New ListItem(ddl_ascx.SelectedValue.Replace(".ascx", "Editor.ascx"))) Then
                    ddl_ascx_editor.SelectedValue = ddl_ascx.SelectedValue.Replace(".ascx", "Editor.ascx")
                End If
            End If

            ' Bind Grids
            gvw_hostspecific.DataSource = RegisteredControl.GetRootSpecificControls(Groove.admin_root.root_id)
            gvw_hostspecific.DataBind()

            gvw_shared.DataSource = RegisteredControl.GetSharedControls()
            gvw_shared.DataBind()

        End Sub


        Protected Sub lb_Add_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_add.Click

            If Page.IsValid Then
                Dim ascx_source As String = String.Format("~{0}{1}", ddl_dir.SelectedValue, ddl_ascx.SelectedValue)

                If pnl_editor.Visible Then
                    Dim editor_source As String = String.Format("~{0}{1}", ddl_dir_editor.SelectedValue, ddl_ascx_editor.SelectedValue)
                    RegisteredControl.InsertControl(0, Groove.admin_root.root_id, txt_name.Text, ascx_source, editor_source, True)
                Else
                    RegisteredControl.InsertControl(0, Groove.admin_root.root_id, txt_name.Text, ascx_source, String.Empty)
                End If

            End If

            Groove.RefreshPage()

        End Sub

        Protected Sub gvw_hostspecific_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvw_hostspecific.RowDataBound

            Dim rc As RegisteredControl = CType(e.Row.DataItem, RegisteredControl)
            If rc IsNot Nothing Then


                Dim dwiz As CommitWizard = CType(e.Row.FindControl("dwiz"), CommitWizard)

                Dim lit_used As Literal = CType(e.Row.FindControl("lit_used"), Literal)
                dwiz.CommandArgument = rc.control_id

                If rc.use_count > 0 Then
                    dwiz.Visible = False
                    lit_used.Visible = True
                Else
                    lit_used.Visible = False
                End If


                Dim lb_share As LinkButton = CType(e.Row.FindControl("lb_share"), LinkButton)
                lb_share.CommandArgument = rc.control_id
                lb_share.CommandName = "share"

            End If

        End Sub

        Protected Sub gvw_shared_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvw_shared.RowDataBound
            Dim rc As RegisteredControl = CType(e.Row.DataItem, RegisteredControl)
            If rc IsNot Nothing Then

                Dim lb_removeshare As LinkButton = CType(e.Row.FindControl("lb_removeshare"), LinkButton)
                lb_removeshare.CommandName = "removeshare"
                lb_removeshare.CommandArgument = rc.control_id
                If rc.use_count > 0 Then
                    Dim nc As NodeControl = Nothing
                    Dim roots As New ArrayList
                    For Each nc In rc.node_controls
                        If nc.node.root_id <> Groove.admin_root.root_id Then
                            lb_removeshare.Visible = False
                            Exit For
                        End If
                    Next
                End If
            End If

        End Sub

        Protected Sub lb_sharing_Click(ByVal sender As Object, ByVal e As EventArgs)

            Dim lb As LinkButton = CType(sender, LinkButton)
            Dim control_id As Integer = CType(lb.CommandArgument, Integer)
            Dim rc As RegisteredControl = RegisteredControl.GetRegisteredControl(control_id)
            If lb.CommandName = "share" Then
                rc.root_id = 0
            ElseIf lb.CommandName = "removeshare" Then
                rc.root_id = Groove.admin_root.root_id
            End If
            rc.Update()

            BindPage()


        End Sub


        Protected Sub dwiz_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim dwiz As CommitWizard = CType(sender, CommitWizard)
            Dim control_id As Integer = CType(dwiz.CommandArgument, Integer)
            RegisteredControl.DeleteControl(control_id)
            BindPage()
        End Sub

        Protected Sub chk_has_editor_CheckChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chk_has_editor.CheckedChanged
            pnl_editor.Visible = chk_has_editor.Checked
        End Sub

        Protected Sub ddl_dir_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddl_dir.SelectedIndexChanged
            ddl_dir_editor.SelectedValue = ddl_dir.SelectedValue
            ddl_ascx.Items.Clear()
            ddl_ascx_editor.Items.Clear()
            BindPage()
        End Sub

        Protected Sub ddl_dir_editor_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddl_dir_editor.SelectedIndexChanged
            ddl_ascx_editor.Items.Clear()
            If ddl_dir_editor.SelectedValue = ddl_dir.SelectedValue Then ddl_ascx.Items.Clear()
            BindPage()
        End Sub


    End Class

End Namespace